var gulp = require('gulp'),
    concat = require('gulp-concat'),
    dgbl = require("del-gulpsass-blank-lines"), //删除生成的css文件的空行,
    sass = require('gulp-sass'),
    rename = require('gulp-rename'),
    uglify = require('gulp-uglify'),
    minifyCSS = require('gulp-minify-css'),
    autoprefixer = require('gulp-autoprefixer'),
    paths = {
        build: {
            root: 'src/',
            styles: 'src/scss/',
            scripts: 'src/js/'
        },
        dist: {
            root: 'dist/',
            styles: 'dist/css/',
            scripts: 'dist/js/'
        }
    },
    loader = {
        filename: "jquery.loader",
        jsFiles: [
            'src/js/jquery.loader.js',
        ]
    };


gulp.task('sass', function () {
    gulp.src(paths.build.styles + loader.filename + '.scss')
        .pipe(sass({
                outputStyle: 'compact'
            })
            .on('error', sass.logError))
        .pipe(dgbl())
        .pipe(autoprefixer({
            browsers: ['last 1 Chrome versions', 'Android >= 4.0', 'iOS 7'],
            cascade: true, //是否美化属性值 默认：true 像这样：
            remove: true //是否去掉不必要的前缀 默认：true 
        }))
        .pipe(gulp.dest(paths.dist.styles))
        .pipe(minifyCSS())
        .pipe(rename(function (path) {
            path.basename = loader.filename + '.min';
        }))
        .pipe(gulp.dest(paths.dist.styles));
    console.log("info-", "scss编译完成");
});
gulp.task('testAutoFx', function () {
    gulp.src([paths.dist.styles + loader.filename + '.css', paths.dist.styles + loader.filename + '.min.css'])
        .pipe(autoprefixer({
            browsers: ['last 1 Chrome versions', 'Android >= 4.0', 'iOS 7'],
            cascade: true, //是否美化属性值 默认：true 像这样：
            remove: true //是否去掉不必要的前缀 默认：true 
        }))
        .pipe(gulp.dest(paths.dist.styles));
});

gulp.task("js", function () {
    gulp.src(loader.jsFiles)
        .pipe(concat(loader.filename + '.js'))
        .pipe(gulp.dest(paths.dist.scripts))
        .pipe(uglify())
        .pipe(rename(function (path) {
            path.basename = loader.filename + '.min';
        }))
        .pipe(gulp.dest(paths.dist.scripts));
});
gulp.task('dist', function () {
    gulp.src(paths.dist.scripts + loader.filename + '.js')
        .pipe(uglify())
        .pipe(rename(function (path) {
            path.basename = loader.filename + '.min';
        }))
        .pipe(gulp.dest(paths.dist.scripts));
});
//压缩css
gulp.task('minFileCss', function () {
    console.log("info-", "压缩css");
    gulp.src(paths.dist.styles + loader.filename + '.css')
        .pipe(minifyCSS())
        .pipe(rename(function (path) {
            path.basename = loader.filename + '.min';
        }))
        .pipe(gulp.dest(paths.dist.styles));
});
gulp.task('watch', function () {
    gulp.watch([paths.build.styles + '*.scss', paths.build.styles + '**/*.scss'], ['sass']);
    gulp.watch([paths.build.scripts + '*.js', paths.build.scripts + '**/*.js'], ['js']);
});
gulp.task('clean', function (cb) {
    console.log("info", "清除文件...")
    del([paths.dist.styles], cb)
});


//Default Task
gulp.task('start', ['sass', 'js', 'watch']);